
package com.xifan.myaccount.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.xifan.myaccount.R;
import com.xifan.myaccount.widget.CalcListener;
import com.xifan.myaccount.widget.MoneyView;

import java.util.Calendar;

public class UiHelper {

    public static final int REQUEST_SELECT_CAMERA = 0;
    public static final int REQUEST_SELECT_PICTURE = 1;
    public static final int REQUEST_SELECT_NONE = 2;
    public static final int REQUEST_GET_TYPE_INFO = 2;
    public static final int START_ADD_RECORD = 3;
    public static final int START_EDIT_RECORD = 4;
    public static final int START_ACCOUNT_MANAGER = 5;
    public static final int START_SETTINGS = 6;

    private static final int[] BUTTONS = new int[] {
            R.id.one, R.id.two, R.id.three, R.id.four, R.id.five,
            R.id.six, R.id.seven, R.id.eight, R.id.nine, R.id.zero, R.id.add, R.id.sub, R.id.mul,
            R.id.div, R.id.dot, R.id.equal, R.id.clear
    };

    public static AlertDialog createViewDialog(Context context,
            OnClickListener listener, View view, String title) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(view)
                .setPositiveButton(context.getResources().getString(R.string.ok), listener)
                .setNegativeButton(context.getResources().getString(R.string.cancel), listener)
                .create();
        return dialog;
    }

    public static AlertDialog createViewDialog(Context context,
            OnClickListener listener, View view, String title, String posStr,
            String negStr) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(view)
                .setPositiveButton(posStr, listener)
                .setNegativeButton(negStr, listener)
                .create();
        return dialog;
    }

    public static AlertDialog createMonthPickerDialog(Context context, OnClickListener listener,
            OnValueChangeListener valueListener,
            int selectedMonth, String title) {
        View monthView = View.inflate(context, R.layout.view_month_picker, null);
        final NumberPicker monthPicker = (NumberPicker) monthView
                .findViewById(R.id.month_picker);
        monthPicker.setDisplayedValues(context.getResources().getStringArray(R.array.months));
        monthPicker.setMinValue(1);
        monthPicker.setMaxValue(12);
        if (valueListener != null)
            monthPicker.setOnValueChangedListener(valueListener);
        if (selectedMonth == -1) {
            monthPicker.setValue(Util.getMonth() + 1);
        } else {
            monthPicker.setValue(selectedMonth + 1);
        }

        return createViewDialog(context, listener, monthView, title);
    }

    public static AlertDialog createListViewDialog(Context context,
            OnClickListener listener, OnItemClickListener itemListener,
            String[] itemList, String title, String buttonPos, String buttonNeg) {
        View view = View.inflate(context, R.layout.view_common_list, null);
        ListView listView = (ListView) view.findViewById(R.id.item_list);
        listView.setAdapter(new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_single_choice,
                itemList));
        listView.setOnItemClickListener(itemListener);
        listView.setSelection(0);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        view.setLayoutParams(params);
        AlertDialog dialog = createViewDialog(context, listener, view, title, buttonPos, buttonNeg);
        return dialog;
    }

    public static AlertDialog createPhotoDialog(Context context, String[] items,
            OnClickListener listener) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(context.getResources().getString(R.string.show_record_choose_pic_source))
                .setItems(items, listener).create();
        return dialog;
    }

    public static AlertDialog createDateDialog(Context context, final Calendar calendar,
            OnClickListener listener) {
        View view = View.inflate(context, R.layout.view_datetime_picker, null);
        DatePicker mDatePicker = (DatePicker) view.findViewById(R.id.datepicker);
        TimePicker mTimePicker = (TimePicker) view.findViewById(R.id.timepicker);
        mDatePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                new DatePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker view, int year,
                            int monthOfYear,
                            int dayOfMonth) {
                        calendar.set(year, monthOfYear, dayOfMonth);
                    }
                });
        mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);

            }
        });
        AlertDialog dialog = createViewDialog(context, listener, view,
                context.getResources().getString(R.string.msg_input_date));
        return dialog;
    }

    public static PopupWindow createNumberPadWindow(Context context, MoneyView moneyText) {
        final PopupWindow mNumberPad;
        View popView = View.inflate(context, R.layout.number_pad, null);
        TextView calcOperator = (TextView) popView.findViewById(R.id.operator);
        TextView calcText = (TextView) popView.findViewById(R.id.new_number);
        CalcListener calcListener = new CalcListener(moneyText, calcText,
                calcOperator);
        for (int id : BUTTONS) {
            ((Button) popView.findViewById(id)).setOnClickListener(calcListener);
        }
        mNumberPad = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
                false);
        mNumberPad.setBackgroundDrawable(new ColorDrawable());
        return mNumberPad;
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}
